// pages/anchor/Pdynamic/Pdynamic.js
var apiUpLoad = 'https://www.uhuijia.com.cn/';
var images = new Array();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  uploadImg: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;

    if (images.length >= 6) {
      that.setData({
        showAlerts: "最多添加6张图片",
        showAlert: true,
        showPic: false
      })
    } else {
      wx.chooseImage({
        count: 6,
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFiles[0].path;
          console.log(res);
          for (var i = 0; i < res.tempFiles.length; i++) {
            that.upload(that, res.tempFiles[i].path, index);
          }
          // that.setData({
          //   images: images
          // })
          // that.upload(that, tempFilePaths,index);
        }
      })
    }
  },
  //商品图片传送到外网
  upload: function (page, path, index) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
        url: apiUpLoad + "image-server/uploadImage/oss",
        filePath: path,
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {
          //和服务器约定的token, 一般也可以放在header中
          'session_token': wx.getStorageSync('session_token')
        },
        success: function (res) {
          console.log(res);
          // var images = that.data.images;
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var data = res.data;
          var aa = JSON.parse(data); //page.stringToJson(data);  //字符串转json
          var imgUrl = aa.data.url;
          images.push(imgUrl);
          page.setData({  //上传成功修改显示头像
            imgUrl: imgUrl,
            images: images
          })

        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  deleteImg: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var picurl = e.currentTarget.dataset.num;
    //根据picurl确定是哪个数组
    that.getindex(picurl);
    console.log(e);
    images.splice(index, 1);
    if (images.length < 6) {
      that.setData({
        showPic: true
      })
    }
    this.setData({
      images: images
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})